﻿using System;
using AllInOnePluginBL.SiteScrappers;

namespace AllInOnePluginBL.jobs
{
    /// <summary>
    /// Summary description for SitesScrapperJob
    /// </summary>
    public class SitesScrapperJob : BaseScrapperJob
    {
        #region Private const
        private const int PAGES_TO_SCAN = 3;
        private const int RUN_TIME_INTERVAL_IN_HOURS = 2; 
        #endregion

        #region Ctor
        public SitesScrapperJob()
            : base(RUN_TIME_INTERVAL_IN_HOURS, JobType.SiteScrapper)
        {
            _siteScappers.Add(new MyVodTVParserJob());
            _siteScappers.Add(new TopDownMeParserJob());
            _siteScappers.Add(new SartababritParserJob());
            _siteScappers.Add(new VidicsScrapper());
        } 
        #endregion

        #region Implementation of IJob
        public override void Execute()
        {
            foreach (var siteScapper in _siteScappers)
            {
                RunSiteScrapper(siteScapper);
            }

            UpdateMainJobRunTime();
        }
        #endregion

        #region Private Methods
        private void RunSiteScrapper(ISiteScapper siteScapper)
        {
            try
            {
                _log.Info(string.Format("Start Executing SitesScrapperJob at {0}", DateTime.Now.ToString("r")));

                siteScapper.Execute(PAGES_TO_SCAN);

                _log.Info(string.Format("End Executing SitesScrapperJob at {0}", DateTime.Now.ToString("r")));
            }
            catch (Exception ex)
            {
                _log.ErrorException("Error Executing siteScapper", ex);
            }
            finally
            {
                UpdateSiteScapperJobRunTime(siteScapper);
            }
        } 
        #endregion
    }
}